package com.bestom.bluetoothlock.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.bestom.bluetoothlock.R;
import com.bestom.bluetoothlock.bluetooth.BleServiceManage;
import com.bestom.bluetoothlock.entity.BleInfo;
import com.bestom.bluetoothlock.util.Constants;
import com.bestom.bluetoothlock.util.ToastUtil;
import com.bestom.bluetoothlock.views.SearchDialog;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchGuidanceActivity extends BaseActivity {
    private BleServiceManage bleManage;
    private SearchDialog dialog;
    @SuppressLint("HandlerLeak")
    public Handler handlerListen = new Handler() {

        public void handleMessage(Message message) {
            super.handleMessage(message);
            int i = message.what;
            if (i == 0) {
                SearchGuidanceActivity.this.handlerListen.removeMessages(100);
                SearchGuidanceActivity.this.list.add((BleInfo) message.obj);
                Intent intent = new Intent(SearchGuidanceActivity.this, SearchDeviceActivity.class);
                intent.putExtra(Constants.KEY_VALUE, (Serializable) SearchGuidanceActivity.this.list);
                startActivity(intent);
            } else if (i == 100) {
                Intent intent2 = new Intent(SearchGuidanceActivity.this, SearchDeviceActivity.class);
                intent2.putExtra(Constants.KEY_VALUE, (Serializable) SearchGuidanceActivity.this.list);
                startActivity(intent2);
            }
        }
    };

    public List<BleInfo> list = new ArrayList<>();

    @BindView(R.id.tv_title)
    TextView tv_title;


    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_search_guidance);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        this.tv_title.setText(getString(R.string.search_guidance));
        this.bleManage = BleServiceManage.getInstance(this);
        this.bleManage.setHandlerListener(this.handlerListen);
    }

    private void next() {
        if (!this.bleManage.checkBLEenable()) {
            ToastUtil.toast(getString(R.string.please_open_bluetooth));
            return;
        }
        this.bleManage.startScan();
        showSearchDialog();
        this.handlerListen.sendEmptyMessageDelayed(100, 10000);
    }

    private void showSearchDialog() {
        this.dialog = new SearchDialog(this);
        this.dialog.show();
        this.dialog.setTitle(getString(R.string.are_the_search));
        Window window = this.dialog.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = -1;
        window.setAttributes(attributes);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    @OnClick({R.id.iv_left, R.id.tv_next})
    public void onClickView(View view) {
        int id = view.getId();
        if (id == R.id.iv_left) {
            finish();
        } else if (id == R.id.tv_next) {
            next();
        }
    }
}
